/*     */ package com.cr.generator.service.impl;
/*     */ 
/*     */ import com.alibaba.fastjson.JSON;
/*     */ import com.alibaba.fastjson.JSONObject;
/*     */ import com.cr.common.core.text.Convert;
/*     */ import com.cr.common.exception.ServiceException;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.generator.domain.GenTable;
/*     */ import com.cr.generator.domain.GenTableColumn;
/*     */ import com.cr.generator.mapper.GenTableColumnMapper;
/*     */ import com.cr.generator.mapper.GenTableMapper;
/*     */ import com.cr.generator.service.IGenTableService;
/*     */ import com.cr.generator.util.GenUtils;
/*     */ import com.cr.generator.util.VelocityInitializer;
/*     */ import com.cr.generator.util.VelocityUtils;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.StringWriter;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.stream.Collectors;
/*     */ import java.util.zip.ZipEntry;
/*     */ import java.util.zip.ZipOutputStream;
/*     */ import org.apache.commons.io.FileUtils;
/*     */ import org.apache.commons.io.IOUtils;
/*     */ import org.apache.velocity.Template;
/*     */ import org.apache.velocity.VelocityContext;
/*     */ import org.apache.velocity.app.Velocity;
/*     */ import org.apache.velocity.context.Context;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Service;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Service
/*     */ public class GenTableServiceImpl implements IGenTableService {
/*  48 */   private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class);
/*     */   
/*     */   @Autowired
/*     */   private GenTableMapper genTableMapper;
/*     */   
/*     */   @Autowired
/*     */   private GenTableColumnMapper genTableColumnMapper;
/*     */   
/*     */   public GenTable selectGenTableById(Long id) {
/*  65 */     GenTable genTable = this.genTableMapper.selectGenTableById(id);
/*  66 */     setTableFromOptions(genTable);
/*  67 */     return genTable;
/*     */   }
/*     */   
/*     */   public List<GenTable> selectGenTableList(GenTable genTable) {
/*  79 */     return this.genTableMapper.selectGenTableList(genTable);
/*     */   }
/*     */   
/*     */   public List<GenTable> selectDbTableList(GenTable genTable) {
/*  91 */     return this.genTableMapper.selectDbTableList(genTable);
/*     */   }
/*     */   
/*     */   public List<GenTable> selectDbTableListByNames(String[] tableNames) {
/* 103 */     return this.genTableMapper.selectDbTableListByNames(tableNames);
/*     */   }
/*     */   
/*     */   public List<GenTable> selectGenTableAll() {
/* 114 */     return this.genTableMapper.selectGenTableAll();
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public void updateGenTable(GenTable genTable) {
/* 127 */     String options = JSON.toJSONString(genTable.getParams());
/* 128 */     genTable.setOptions(options);
/* 129 */     int row = this.genTableMapper.updateGenTable(genTable);
/* 130 */     if (row > 0)
/* 132 */       for (GenTableColumn cenTableColumn : genTable.getColumns())
/* 134 */         this.genTableColumnMapper.updateGenTableColumn(cenTableColumn);  
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public void deleteGenTableByIds(String ids) {
/* 149 */     this.genTableMapper.deleteGenTableByIds(Convert.toLongArray(ids));
/* 150 */     this.genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids));
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public void importGenTable(List<GenTable> tableList, String operName) {
/*     */     try {
/* 165 */       for (GenTable table : tableList) {
/* 167 */         String tableName = table.getTableName();
/* 168 */         GenUtils.initTable(table, operName);
/* 169 */         int row = this.genTableMapper.insertGenTable(table);
/* 170 */         if (row > 0) {
/* 173 */           List<GenTableColumn> genTableColumns = this.genTableColumnMapper.selectDbTableColumnsByName(tableName);
/* 174 */           for (GenTableColumn column : genTableColumns) {
/* 176 */             GenUtils.initColumnField(column, table);
/* 177 */             this.genTableColumnMapper.insertGenTableColumn(column);
/*     */           } 
/*     */         } 
/*     */       } 
/* 182 */     } catch (Exception e) {
/* 184 */       throw new ServiceException("导入失败：" + e.getMessage());
/*     */     } 
/*     */   }
/*     */   
/*     */   public Map<String, String> previewCode(Long tableId) {
/* 197 */     Map<String, String> dataMap = new LinkedHashMap<>();
/* 199 */     GenTable table = this.genTableMapper.selectGenTableById(tableId);
/* 201 */     setSubTable(table);
/* 203 */     setPkColumn(table);
/* 204 */     VelocityInitializer.initVelocity();
/* 206 */     VelocityContext context = VelocityUtils.prepareContext(table);
/* 209 */     List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
/* 210 */     for (String template : templates) {
/* 213 */       StringWriter sw = new StringWriter();
/* 214 */       Template tpl = Velocity.getTemplate(template, "UTF-8");
/* 215 */       tpl.merge((Context)context, sw);
/* 216 */       dataMap.put(template, sw.toString());
/*     */     } 
/* 218 */     return dataMap;
/*     */   }
/*     */   
/*     */   public byte[] downloadCode(String tableName) {
/* 230 */     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
/* 231 */     ZipOutputStream zip = new ZipOutputStream(outputStream);
/* 232 */     generatorCode(tableName, zip);
/* 233 */     IOUtils.closeQuietly(zip);
/* 234 */     return outputStream.toByteArray();
/*     */   }
/*     */   
/*     */   public void generatorCode(String tableName) {
/* 246 */     GenTable table = this.genTableMapper.selectGenTableByName(tableName);
/* 248 */     setSubTable(table);
/* 250 */     setPkColumn(table);
/* 252 */     VelocityInitializer.initVelocity();
/* 254 */     VelocityContext context = VelocityUtils.prepareContext(table);
/* 257 */     List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
/* 258 */     for (String template : templates) {
/* 260 */       if (!MyStringUtils.contains(template, "sql.vm")) {
/* 263 */         StringWriter sw = new StringWriter();
/* 264 */         Template tpl = Velocity.getTemplate(template, "UTF-8");
/* 265 */         tpl.merge((Context)context, sw);
/*     */         try {
/* 268 */           String path = getGenPath(table, template);
/* 269 */           FileUtils.writeStringToFile(new File(path), sw.toString(), "UTF-8");
/* 271 */         } catch (IOException e) {
/* 273 */           throw new ServiceException("渲染模板失败，表名：" + table.getTableName());
/*     */         } 
/*     */       } 
/*     */     } 
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public void synchDb(String tableName) {
/* 288 */     GenTable table = this.genTableMapper.selectGenTableByName(tableName);
/* 289 */     List<GenTableColumn> tableColumns = table.getColumns();
/* 290 */     List<String> tableColumnNames = (List<String>)tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
/* 292 */     List<GenTableColumn> dbTableColumns = this.genTableColumnMapper.selectDbTableColumnsByName(tableName);
/* 293 */     if (MyStringUtils.isEmpty(dbTableColumns))
/* 295 */       throw new ServiceException("同步数据失败，原表结构不存在"); 
/* 297 */     List<String> dbTableColumnNames = (List<String>)dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
/* 299 */     dbTableColumns.forEach(column -> {
/*     */           if (!tableColumnNames.contains(column.getColumnName())) {
/*     */             GenUtils.initColumnField(column, table);
/*     */             this.genTableColumnMapper.insertGenTableColumn(column);
/*     */           } 
/*     */         });
/* 308 */     List<GenTableColumn> delColumns = (List<GenTableColumn>)tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
/* 309 */     if (MyStringUtils.isNotEmpty(delColumns))
/* 311 */       this.genTableColumnMapper.deleteGenTableColumns(delColumns); 
/*     */   }
/*     */   
/*     */   public byte[] downloadCode(String[] tableNames) {
/* 324 */     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
/* 325 */     ZipOutputStream zip = new ZipOutputStream(outputStream);
/* 326 */     for (String tableName : tableNames)
/* 328 */       generatorCode(tableName, zip); 
/* 330 */     IOUtils.closeQuietly(zip);
/* 331 */     return outputStream.toByteArray();
/*     */   }
/*     */   
/*     */   private void generatorCode(String tableName, ZipOutputStream zip) {
/* 340 */     GenTable table = this.genTableMapper.selectGenTableByName(tableName);
/* 342 */     setSubTable(table);
/* 344 */     setPkColumn(table);
/* 346 */     VelocityInitializer.initVelocity();
/* 348 */     VelocityContext context = VelocityUtils.prepareContext(table);
/* 351 */     List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
/* 352 */     for (String template : templates) {
/* 355 */       StringWriter sw = new StringWriter();
/* 356 */       Template tpl = Velocity.getTemplate(template, "UTF-8");
/* 357 */       tpl.merge((Context)context, sw);
/*     */       try {
/* 361 */         zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
/* 362 */         IOUtils.write(sw.toString(), zip, "UTF-8");
/* 363 */         IOUtils.closeQuietly(sw);
/* 364 */         zip.flush();
/* 365 */         zip.closeEntry();
/* 367 */       } catch (IOException e) {
/* 369 */         log.error("渲染模板失败，表名：" + table.getTableName(), e);
/*     */       } 
/*     */     } 
/*     */   }
/*     */   
/*     */   public void validateEdit(GenTable genTable) {
/* 382 */     if ("tree".equals(genTable.getTplCategory())) {
/* 384 */       String options = JSON.toJSONString(genTable.getParams());
/* 385 */       JSONObject paramsObj = JSONObject.parseObject(options);
/* 386 */       if (MyStringUtils.isEmpty(paramsObj.getString("treeCode")))
/* 388 */         throw new ServiceException("树编码字段不能为空"); 
/* 390 */       if (MyStringUtils.isEmpty(paramsObj.getString("treeParentCode")))
/* 392 */         throw new ServiceException("树父编码字段不能为空"); 
/* 394 */       if (MyStringUtils.isEmpty(paramsObj.getString("treeName")))
/* 396 */         throw new ServiceException("树名称字段不能为空"); 
/* 399 */     } else if ("sub".equals(genTable.getTplCategory())) {
/* 401 */       if (MyStringUtils.isEmpty(genTable.getSubTableName()))
/* 403 */         throw new ServiceException("关联子表的表名不能为空"); 
/* 405 */       if (MyStringUtils.isEmpty(genTable.getSubTableFkName()))
/* 407 */         throw new ServiceException("子表关联的外键名不能为空"); 
/*     */     } 
/*     */   }
/*     */   
/*     */   public void setPkColumn(GenTable table) {
/* 419 */     for (GenTableColumn column : table.getColumns()) {
/* 421 */       if (column.isPk()) {
/* 423 */         table.setPkColumn(column);
/*     */         break;
/*     */       } 
/*     */     } 
/* 427 */     if (MyStringUtils.isNull(table.getPkColumn()))
/* 429 */       table.setPkColumn(table.getColumns().get(0)); 
/* 431 */     if ("sub".equals(table.getTplCategory())) {
/* 433 */       for (GenTableColumn column : table.getSubTable().getColumns()) {
/* 435 */         if (column.isPk()) {
/* 437 */           table.getSubTable().setPkColumn(column);
/*     */           break;
/*     */         } 
/*     */       } 
/* 441 */       if (MyStringUtils.isNull(table.getSubTable().getPkColumn()))
/* 443 */         table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0)); 
/*     */     } 
/*     */   }
/*     */   
/*     */   public void setSubTable(GenTable table) {
/* 455 */     String subTableName = table.getSubTableName();
/* 456 */     if (MyStringUtils.isNotEmpty(subTableName))
/* 458 */       table.setSubTable(this.genTableMapper.selectGenTableByName(subTableName)); 
/*     */   }
/*     */   
/*     */   public void setTableFromOptions(GenTable genTable) {
/* 469 */     JSONObject paramsObj = JSONObject.parseObject(genTable.getOptions());
/* 470 */     if (MyStringUtils.isNotNull(paramsObj)) {
/* 472 */       String treeCode = paramsObj.getString("treeCode");
/* 473 */       String treeParentCode = paramsObj.getString("treeParentCode");
/* 474 */       String treeName = paramsObj.getString("treeName");
/* 475 */       String parentMenuId = paramsObj.getString("parentMenuId");
/* 476 */       String parentMenuName = paramsObj.getString("parentMenuName");
/* 478 */       genTable.setTreeCode(treeCode);
/* 479 */       genTable.setTreeParentCode(treeParentCode);
/* 480 */       genTable.setTreeName(treeName);
/* 481 */       genTable.setParentMenuId(parentMenuId);
/* 482 */       genTable.setParentMenuName(parentMenuName);
/*     */     } 
/*     */   }
/*     */   
/*     */   public static String getGenPath(GenTable table, String template) {
/* 495 */     String genPath = table.getGenPath();
/* 496 */     if (MyStringUtils.equals(genPath, "/"))
/* 498 */       return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table); 
/* 500 */     return genPath + File.separator + VelocityUtils.getFileName(template, table);
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-generator-4.7.0.jar!\com\cr\generator\service\impl\GenTableServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */